//
//  LSZVideoDetailsWebView.m
//  JK九酷云头条
//
//  Created by 九酷 on 2017/11/8.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZVideoDetailsWebView.h"

@interface LSZVideoDetailsWebView ()

@property (nonatomic, strong) UIProgressView *loadingProgressView;

@end

@implementation LSZVideoDetailsWebView

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@",self.titleStr];
    if ([self.strComeFrame isEqualToString:@"1"]) {
        UIBarButtonItem *button =[[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStyleDone target:self action:@selector(closeConversation)];
        [button setTintColor:RGBColor(89, 196, 120)];
        self.navigationItem.rightBarButtonItem = button;
    }
    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
//    self.webView.
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    //安装进度监听
    [self setupLoadingProgressView];
    [self.view addSubview:self.webView];
    [self setUIShare];
//    if ([self.strComeFrame isEqualToString:@"1"]) {
//        UIBarButtonItem *button =[[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStyleDone target:self action:@selector(closeConversation)];
//        [button setTintColor:RGBColor(89, 196, 120)];
//        self.navigationItem.rightBarButtonItem = button;
//
//        //创建手势 使用initWithTarget:action:的方法创建
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapViewPop:)];
//        //设置属性
//        //tap 手势一共两个属性，一个是设置轻拍次数，一个是设置点击手指个数
//        //设置轻拍次数
//        tap.numberOfTapsRequired = 2;
//        //设置手指字数
//        tap.numberOfTouchesRequired = 1;
//        //别忘了添加到testView上
//        self.view.userInteractionEnabled = YES;
//        [self.view addGestureRecognizer:tap];
//    }
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    NSString *strURL = (navigationAction.request.URL.absoluteString);
    NSLog(@"strURL = %@", strURL);
    self.url = strURL;
     // 允许跳转
    decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)dealloc{
    if([self.isDistinguish isEqualToString:@"1"]){
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
    }
    [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [_webView removeObserver:self forKeyPath:@"title"];
    [_webView stopLoading];
    self.loadingProgressView.hidden = YES;
    [self.loadingProgressView removeFromSuperview];
}
///安装进度监听
- (void)setupLoadingProgressView {
    [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
    [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
}
///设置进度条
- (UIProgressView*)loadingProgressView {
    if (!_loadingProgressView) {
        _loadingProgressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, [[UIScreen mainScreen] bounds].size.width, 2)];
        _loadingProgressView.trackTintColor = [UIColor clearColor];
        _loadingProgressView.progressTintColor = [UIColor colorWithRed:(64)/255.0 green:(181)/255.0 blue:(116)/255.0 alpha:1];
        [self.navigationController.navigationBar addSubview:_loadingProgressView];
    }
    return _loadingProgressView;
}
///设置进度条
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        [self.loadingProgressView setProgress:[change[@"new"] floatValue] animated:YES];
        if (_loadingProgressView.progress == 1.0) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                _loadingProgressView.hidden = YES;
//                _loadingProgressView.progress = 0.0;
            });
        }
    }
    else   if ([keyPath isEqualToString:@"title"])
    {
        if (object == self.webView) {
            NSLog(@"%@><?",self.webView.title);
            self.webString = @"";
            self.webString = self.webView.title;
        }
        else
        {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
-(void)setUIShare
{
    self.viewShare = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight-150, ScreenWidth, 150)];
    self.viewShare.hidden = YES;
    self.viewShare.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.viewShare];
    
    UIButton * butpy = [[UIButton alloc]initWithFrame:CGRectMake(20, 10, 80, 80)];
    [butpy setImage:[UIImage imageNamed:@"分享_微信好友"] forState:UIControlStateNormal];
    [butpy addTarget:self action:@selector(clickPyBut) forControlEvents:UIControlEventTouchUpInside];
    [self.viewShare addSubview:butpy];
    
    UIButton * butCircleFriends = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(butpy.frame)+20, 10, 80, 80)];
    [butCircleFriends setImage:[UIImage imageNamed:@"分享_朋友圈"] forState:UIControlStateNormal];
    [butCircleFriends addTarget:self action:@selector(clickButCircleFriends) forControlEvents:UIControlEventTouchUpInside];
    [self.viewShare addSubview:butCircleFriends];
    
    UIButton * buttonCancelShare = [[UIButton alloc]initWithFrame:CGRectMake(0, self.viewShare.frame.size.height-50, ScreenWidth, 50)];
    buttonCancelShare.backgroundColor = RGBColor(89, 196, 120);
    [buttonCancelShare setTitle:@"取消" forState:UIControlStateNormal];
    [buttonCancelShare addTarget:self action:@selector(clickCancelShare) forControlEvents:UIControlEventTouchUpInside];
    [self.viewShare addSubview:buttonCancelShare];
}

-(void)clickCancelShare
{
    self.viewShare.hidden = YES;
}
-(void)clickPyBut
{
    [LSZSpecialPackaging shareWXWithWebPage:self.url title:self.webString type:0];
    //    [LSZSpecialPackaging shareWXWithViewController:self.shareURL title:self.titleStr type:0];
}
-(void)clickButCircleFriends
{
    [LSZSpecialPackaging shareWXWithWebPage:self.url title:self.webString type:1];
    //    [LSZSpecialPackaging shareWXWithViewController:self.shareURL title:self.titleStr type:1];
}
-(void)closeConversation
{
    NSLog(@"url>>>>>>>%@",self.url);
    if (self.viewShare.hidden == YES) {
        self.viewShare.hidden = NO;
    }else{
        self.viewShare.hidden = YES;
    }
}
//-(void)tapViewPop:(UITapGestureRecognizer *)sender{
//    [self.navigationController popViewControllerAnimated:YES];
//}
@end
